﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class PrintControlDA : IPrintControlDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DOC_ID = "@DOC_ID";
        private string DOC_LINE_ID = "@DOC_LINE_ID";
        private string IS_PRINTED = "@IS_PRINTED";
        private string PRINT_COUNT = "@PRINT_COUNT";
        private string PRINT_TYPE = "@PRINT_TYPE";
        private string SQL_CHECK_PRINT_CONTROL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@PRINT_CONTROL WHERE PRINT_TYPE = @PRINT_TYPE AND DOC_LINE_ID = @DOC_LINE_ID ";
        private string SQL_DELETE_PRINT_CONTROL = " DELETE FROM @_@PRINT_CONTROL WHERE PRINT_TYPE = @PRINT_TYPE AND DOC_ID = @DOC_ID AND DOC_LINE_ID = @DOC_LINE_ID ";
        private string SQL_INSERT_PRINT_CONTROL = " INSERT INTO @_@PRINT_CONTROL ( WH_ID, PRINT_TYPE, DOC_ID, DOC_LINE_ID, IS_PRINTED, PRINT_COUNT, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @PRINT_TYPE, @DOC_ID, @DOC_LINE_ID, @IS_PRINTED, @PRINT_COUNT, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_PRINT_CONTROL = " SELECT  ROWID, WH_ID, PRINT_TYPE, DOC_ID, DOC_LINE_ID, IS_PRINTED, PRINT_COUNT, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@PRINT_CONTROL ";
        private string SQL_UPDATE_PRINT_CONTROL = " UPDATE @_@PRINT_CONTROL SET WH_ID = @WH_ID, IS_PRINTED = @IS_PRINTED, PRINT_COUNT = @PRINT_COUNT, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PRINT_TYPE = @PRINT_TYPE AND DOC_LINE_ID = @DOC_LINE_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckPrintControlIDUnique(string printType, string docLineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PRINT_TYPE", printType), new SqlParameter("@DOC_LINE_ID", docLineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_PRINT_CONTROL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckPrintControlIDUnique(DataBase dataBase, DbTransaction tran, string printType, string docLineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PRINT_TYPE", printType), new SqlParameter("@DOC_LINE_ID", docLineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_PRINT_CONTROL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public DataSet GetAllPrintControl(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PRINT_CONTROL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllPrintControl(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PRINT_CONTROL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public PrintControlInfo GetPrintControlByID(string printType, string docLineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PRINT_CONTROL.Replace("@_@", whLoginID) + " WHERE PRINT_TYPE = @PRINT_TYPE AND DOC_LINE_ID = @DOC_LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PRINT_TYPE", printType), new SqlParameter("@DOC_LINE_ID", docLineID) };
            PrintControlInfo printControlInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    printControlInfo = new PrintControlInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PRINT_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["DOC_LINE_ID"].ToString(), reader["IS_PRINTED"].ToString(), (reader["PRINT_COUNT"].ToString() != "") ? int.Parse(reader["PRINT_COUNT"].ToString()) : 0, (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return printControlInfo;
        }

        public PrintControlInfo GetPrintControlByID(DataBase dataBase, DbTransaction tran, string printType, string docLineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_PRINT_CONTROL.Replace("@_@", whLoginID) + " WHERE PRINT_TYPE = @PRINT_TYPE AND DOC_LINE_ID = @DOC_LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PRINT_TYPE", printType), new SqlParameter("@DOC_LINE_ID", docLineID) };
            PrintControlInfo printControlInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                printControlInfo = new PrintControlInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PRINT_TYPE"].ToString(), reader["DOC_ID"].ToString(), reader["DOC_LINE_ID"].ToString(), reader["IS_PRINTED"].ToString(), (reader["PRINT_COUNT"].ToString() != "") ? int.Parse(reader["PRINT_COUNT"].ToString()) : 0, (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return printControlInfo;
        }

        public DataSet GetPrintControlByQueryList(List<string> sqlWhere, PrintControlQueryEntity printcontrolQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_PRINT_CONTROL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!printcontrolQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, printcontrolQuery.CurrentPage, printcontrolQuery.PageSize, printcontrolQuery.SortField, printcontrolQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetPrintControlByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, PrintControlQueryEntity printcontrolQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_PRINT_CONTROL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, printcontrolQuery.CurrentPage, printcontrolQuery.PageSize, printcontrolQuery.SortField, printcontrolQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertPrintControl(PrintControlInfo printControlInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                printControlInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_PrintControl_Parameters(printControlInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PRINT_CONTROL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertPrintControl(DataBase dataBase, DbTransaction tran, PrintControlInfo printControlInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                printControlInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_PrintControl_Parameters(printControlInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PRINT_CONTROL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_PrintControl_Parameters(PrintControlInfo printControlInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.PRINT_TYPE, SqlDbType.VarChar, 50), new SqlParameter(this.DOC_ID, SqlDbType.VarChar, 50), new SqlParameter(this.DOC_LINE_ID, SqlDbType.VarChar, 50), new SqlParameter(this.IS_PRINTED, SqlDbType.VarChar, 1), new SqlParameter(this.PRINT_COUNT, SqlDbType.Int, 4), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            paramArray[0].Value = printControlInfo.WhID;
            paramArray[1].Value = printControlInfo.PrintType;
            paramArray[2].Value = printControlInfo.DocID;
            paramArray[3].Value = printControlInfo.DocLineID;
            paramArray[4].Value = printControlInfo.IsPrinted;
            paramArray[5].Value = printControlInfo.PrintCount;
            paramArray[6].Value = printControlInfo.CreatedBy;
            paramArray[7].Value = printControlInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdatePrintControl(PrintControlInfo printControlInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                printControlInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_PrintControl_Parameters(printControlInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PRINT_CONTROL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdatePrintControl(DataBase dataBase, DbTransaction tran, PrintControlInfo printControlInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                printControlInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_PrintControl_Parameters(printControlInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PRINT_CONTROL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

